import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {PlanService} from "@train/plan/plan.service";

import {ToolsService} from "@shared/service/tools/tools.service";
import {ActivatedRoute, NavigationExtras, Params, Router} from "@angular/router";
import {AuthService} from "@shared/service/auth/auth.service";
import {TrainTeachInfoVo} from "@interface/index";



@Component({
  selector: 'seed-edit-teaching-information',
  templateUrl: './edit-teaching-information.component.html',
  styleUrls: ['./edit-teaching-information.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class EditTeachingInformationComponent implements OnInit {
  public teachInfo: TrainTeachInfoVo;
  public maps: any;
  public type: string;
  public subjectName: string;
  public teacherName: string;
  public teachProcessDefault: TrainTeachInfoVo = new  TrainTeachInfoVo();
  @ViewChild('focusField') focusField;
  @ViewChild('equipment') equipment;
  @ViewChild('area') area;
  @ViewChild('heartRate') heartRate;
  @ViewChild('density') density;
  @ViewChild('mentalLoad') mentalLoad;
  constructor(
    private _planService: PlanService,
    private _toolService: ToolsService,
    private _route: ActivatedRoute,
    private _authService: AuthService,
    private _router: Router
  ) {
    this.teachInfo = new TrainTeachInfoVo();
    this.maps = {
      focusField: new Set(),
      area: new Set(),
      equipment: new Set(),
    };
    this._route.params
      .switchMap((parmas: Params) => {
        this.teachInfo.classScheduleId = parmas['classScheduleId'];
        this.teachInfo['authOrgId'] = this._authService.getAuthOrgId();
        this.fetchTeachInfo( parmas['classScheduleId']);
        return this._route.queryParams;
      }).subscribe((params: Params) => {
      this.type = params['type'];
      this.teachInfo['id'] = params['id'];
      this.subjectName = params['subjectName'];
      this.teacherName = params['teacherName'];
    });
  }

  /**
   * @description 更新/修改教学信息
   * @author @chenwh576
   * @date 2018/1/3 下午8:56
   */
  updateTeachInfo(): void {
    const query = this.teachInfo;
    // console.log(query, 'update');
    this._planService.updateTeachInfo(query)
      .subscribe(res => {
        if (res.result === '0') {
          this.goToTrainDetailPage();
        } else {
          this._toolService.showToast(res.msg);
        }
      });
  }

  saveTeachInfo() {
    const query = this.teachInfo;
    this._planService.saveTeachInfo(query)
      .subscribe(res => {
        if (res.result === '0') {
          this.goToTrainDetailPage();
        } else {
          this._toolService.showToast(res.msg);
        }
      });
  }

  handleTeachInfoEvent() {
    const data = this.teachInfo;
    const heartRate = +data.heartRate;
    const density = +data.density;
    if ( data.heartRate && (!Number.isInteger(heartRate) || heartRate > 100 || heartRate < 0)) {
      this._toolService.showToast('平均心率 请填写0 - 100 的数字');
      return;
    }
    if ( data.density && (!Number.isInteger(density) || density > 100 || density < 0)) {
      this._toolService.showToast('练习密度 请填写0 - 100 的数字');
      return;
    }
    if (!!this.teachInfo['id']) {
      this.updateTeachInfo();
    } else {
      this.saveTeachInfo();
    }
  }

  /**
   * @description 设置教学目标
   * @author @chenwh576
   * @date 2018/1/3 下午9:01
   */
  setTeachInfoByTarget(query) {
    if (query !== false) {
      this.teachInfo.target = query;
    }
  }

  /**
   * @description 设置重点领域,场地,器材
   * @author @chenwh576
   * @date 2018/1/3 下午9:01
   */
  handleChooseValueByTeachInfo(event, type: string) {
    event.stopPropagation();
    if (event.target.nodeName.toLocaleLowerCase() !== 'li') {
      return;
    }

    switch (type) {
      case 'focusField' :
        if (event.target.classList.contains('active')) {
          event.target.classList.remove('active');
          this.maps.focusField.delete(event.target.innerHTML);
        } else if (!event.target.classList.contains('active')) {
          event.target.classList.add('active');
          this.maps.focusField.add(event.target.innerHTML);
        }

        const value = Array.from(this.maps.focusField).toString();
        this.teachInfo.focusField = value;
        break;
      case 'area' :
        if (event.target.classList.contains('active')) {
          event.target.classList.remove('active');
          this.maps.area.delete(event.target.innerHTML);
        } else if (!event.target.classList.contains('active')) {
          event.target.classList.add('active');
          this.maps.area.add(event.target.innerHTML);
        }

        const area = Array.from(this.maps.area).toString();
        this.teachInfo.area = area;
        break;
      case 'equipment' :
        if (event.target.classList.contains('active')) {
          event.target.classList.remove('active');
          this.maps.equipment.delete(event.target.innerHTML);
        } else if (!event.target.classList.contains('active')) {
          event.target.classList.add('active');
          this.maps.equipment.add(event.target.innerHTML);
        }

        const equipment = Array.from(this.maps.equipment).toString();
        this.teachInfo.equipment = equipment;
        break;
    }

  }


  /**
   * @description 设置平均心率
   * @author @chenwh576
   * @date 2018/1/3 下午9:01
   */
  setTeachInfoByHeartRate(query: number) {
    this.teachInfo.heartRate = query;
  }

  /**
   * @description 设置练习密度
   * @author @chenwh576
   * @date 2018/1/3 下午9:01
   */
  setTeachInfoByDensity(query: number) {
    this.teachInfo.density = query;
  }

  /**
   * @description 设置心理负荷
   * @author @chenwh576
   * @date 2018/1/3 下午9:01
   */
  setTeachInfoByMentalLoad(query: number) {
    this.teachInfo.mentalLoad = query;
  }

  /**
   * @description 前往教学计划详情页面
   * @author @chenwh576
   * @date 2018/1/6 下午4:18
   */
  goToTrainDetailPage() {
    const NavigationExtras: NavigationExtras = {
      queryParams: {
        subjectName: this.subjectName,
        teacherName: this.teacherName
      }
    };
    // this._router.navigate(['/train/plan/detail/', this.teachInfo.classScheduleId], NavigationExtras);
    history.back();
  }

  /**
   * @description 判断teachInfo对象是否有值
   * 有值则允许提交反之不可
   * @author @chenwh576
   * @date 2018/1/6 下午4:18
   */
  get isDisabled() {
     const teachInfo = this.teachInfo;
    if (!!teachInfo.focusField
      || !!teachInfo.target
      || !!teachInfo.area
      || !!teachInfo.mentalLoad
      || !!teachInfo.density
      || !!teachInfo.heartRate
      || !!teachInfo.equipment
    ) {
      return false;
    } else {
      return true;
    }

  }
  /**
   * @description 根据课时ID 获取教学信息
   * @author ouxh862
   * @date 2018.01.02
   * @param {number} scheduleId
   */
  fetchTeachInfo(scheduleId: number) {
    this._planService.fetchTeachInfo(scheduleId)
      .filter(res => res.result === '0')
      .subscribe(res => {
        this.teachProcessDefault = res.data;
        const processDefault = this.teachProcessDefault;
        if (processDefault.focusField) {
          this.teachInfo.focusField = processDefault.focusField;
          this._planService.AdaptSelect(this.focusField, processDefault.focusField);
        }
        if (processDefault.equipment) {
          this.teachInfo.equipment = processDefault.equipment;
          this._planService.AdaptSelect(this.equipment, processDefault.equipment);
        }
        if (processDefault.area) {
          this.teachInfo.area = processDefault.area;
          this._planService.AdaptSelect(this.area, processDefault.area);
        }
        if (processDefault.heartRate) {
          this.teachInfo.heartRate = processDefault.heartRate;
          this.heartRate.nativeElement.value = processDefault.heartRate;
        }
        if (processDefault.mentalLoad) {
          this.teachInfo.mentalLoad = processDefault.mentalLoad;
          this.mentalLoad.nativeElement.value = processDefault.mentalLoad;
        }
        if (processDefault.density) {
          this.teachInfo.density = processDefault.density;
          this.density.nativeElement.value = processDefault.density;
        }
      });
  }
  ngOnInit() {
  }

}
